import 'dart:async';

import 'package:args/command_runner.dart';
import 'package:gate_cli/utils/asset_command_util.dart';

import '../models/res_config.dart';
import '../utils/command_util.dart';

class AssetCommand extends Command<String> {
  AssetCommand() {
    // asset -c config
    argParser.addOption('config', abbr: 'c', help: '配置文件');
    // asset --skip-compress=false
    argParser.addOption('skip-compress', help: '跳过压缩', defaultsTo: 'true');
  }

  @override
  String get description => '资源构建';

  @override
  String get name => 'asset';

  @override
  List<String> get aliases => ['a'];

  @override
  FutureOr<String>? run() {
    final config = CommandUtil.loadConfig(argResults?['config'] ?? "");
    final skipCompress = argResults?['skip-compress'] == 'true';
    final pubspec = CommandUtil.loadConfig('pubspec.yaml');
    String? pkgName = pubspec['name'];
    if (pkgName == null || pkgName.isEmpty) {
      print('无法获取包名，请确认配置文件是否正确');
      return "";
    }
    final resConfig = ResConfig.fromYaml(config);
    final assetConfig = resConfig.assetConfig;
    AssetCommandUtil.instance.loadAssets(pubspec, assetConfig, pkgName, skipCompress);
    return "";
  }
}
